package com.jt.antiFakeSys.helper;

import cn.hutool.core.util.ImageUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.jt.antiFakeSys.utils.CodeImageUtils;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * AntiFakeCodeHelper
 *
 * @author longhp
 * @Date 2019/3/3 9:38
 */
@Component
public class AntiFakeCodeHelper {


    /**
     * 生成下面带文字说明的二维码
     * @param url
     * @param text
     * @param width
     * @param height
     * @param fontSize
     * @return
     */
    public BufferedImage getQrCodeByCode(String url,String text,int width,int height,int fontSize){
        QrConfig qrConfig = new QrConfig(width, height);
        qrConfig.setErrorCorrection(ErrorCorrectionLevel.L);
        qrConfig.setMargin(1);
        BufferedImage bufferedImageQrCode =  QrCodeUtil.generate(url,qrConfig);
        bufferedImageQrCode = ImageUtil.rotate(bufferedImageQrCode,90);
        BufferedImage bufferedImageText = CodeImageUtils.createTextImage(width,text,fontSize);
        return CodeImageUtils.jointPic(bufferedImageQrCode,bufferedImageText);
    }

    public File createQrCodeByCodeFile(String url, String text, int width, int height, int fontSize,String destSrc){
        BufferedImage bufferedImage = getQrCodeByCode(url, text, width, height, fontSize);
        File file = new File(destSrc);
        try {
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdir();
            }
            ImageIO.write(bufferedImage,"png",file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }





}
